<?php

/**
 * @author Martin Skála
 */
class Admin_AutorController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }


    public function indexAction()
    {
        $autori = new Knihovna_Model_Autor();
        $this->view->autori = $autori->FetchAll();

        $flashMessage = $this->_helper->flashMessenger;
        $this->view->message = $flashMessage->getMessages();
    }

    function addAction()
    {
        $form = new Admin_Form_Autor();
        $form->submit->setLabel('Přidej');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {
                $jmeno = $form->getValue('jmeno');
                $prijmeni = $form->getValue('prijmeni');
                $narozeni = $form->getValue('narozeni');

                $autori = new Knihovna_Model_Autor();
                $autori->pridejAutora($jmeno, $prijmeni, $narozeni);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
    }

    function editAction()
    {
        $form = new Admin_Form_Autor();
        $form->submit->setLabel('Uložit');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id_autora');
                $jmeno = $form->getValue('jmeno');
                $prijmeni = $form->getValue('prijmeni');
                $narozeni = $form->getValue('narozeni');

                $autori = new Knihovna_Model_Autor();
                $autori->upravAutora($id, $jmeno, $prijmeni, $narozeni);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id_autora', 0);
            if ($id > 0) {

                $autori = new Knihovna_Model_Autor();
                $row=$autori->najdiAutora($id);
                $form->populate($row);
            }
        }
    }

    public function deleteAction()
    {
        $form = new Admin_Form_DeleteAutor();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try {
                $id = $this->_getParam('id_autora');
                $autori = new Knihovna_Model_Autor();
                $autori->smazAutora($id);
                $this->_helper->flashMessenger('success|Autor byl úspěšně smazán.');
                $this->_helper->redirector('index');
            } catch (Zend_Db_Exception $error) {
                $this->_helper->flashMessenger('error|' . $error->getMessage());
            }
        } elseif ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))) {
            $this->_redirect('/admin/autor/');
        }

        $this->view->form = $form;
    }
}